import {createRouter, createWebHistory} from "vue-router";
import {useLoginStore} from "@/store/index.js";


const routes = [
    {
        name: 'home',
        path: "/",
        component: () => import("@/view/Index.vue"),
        redirect:'/index',
        meta: {
            isAuth: false,
            isShowSearch: true,
            isBlack:true
        },
        children:[
            {
                path:'index',
                name:'main',
                component: () => import("@/view/main/Main.vue"),
                meta:{
                    isBlack:true
                }
            },
            {
                path:'channel/:categoryId',
                name:'channel',
                component: () => import("@/view/channel/ChannelVue.vue"),
                meta:{
                    isBlack:true
                }
            }
        ]
    },

    {
        name:'videoDetail',
        path: "/video/detail/:videoCode",
        component:()=>import("@/view/video/VideoDetail.vue"),
        meta:{
            isBlack:false
        }
    },
    {
        name:'profile',
        path: "/profile",
        redirect: '/profile/info',
        component:()=>import("@/view/profile/Profile.vue"),
        meta:{
            isBlack:false,
            isAuth: true
        },
        children: [
            {
                name:'userInfo',
                path:'info',
                component:()=>import("@/view/profile/component/UserInfo.vue"),
                meta:{
                    isBlack:false,
                    isAuth: true
                }
            },
            {
                name:'collection',
                path:'collection',
                component:()=>import("@/view/profile/component/Collection.vue"),
                meta:{
                    isBlack:false,
                    isAuth: true
                }
            },
            {
                name:'account',
                path:'account',
                component:()=>import("@/view/profile/component/Account.vue"),
                meta:{
                    isBlack:false,
                    isAuth: true
                }
            },
            {
                name:'comment',
                path:'comment',
                component:()=>import("@/view/profile/component/Comment.vue"),
                meta:{
                    isBlack:false,
                    isAuth: true
                }
            }
        ]
    },
    {
        name: 'videoPlayer',
        path: '/play/:videoCode',
        component:()=>import("@/view/video/VideoPlayer.vue"),
        meta:{
            isBlack:false
        }
    },
    {
        name: 'search',
        path: '/search',
        component:()=>import("@/view/search/Search.vue"),
        meta:{
            isBlack:false,
            hiddenHeaderSearch:true
        }
    },
    {
        name:'history',
        path:'/history',
        component:()=>import("@/view/history/History.vue"),
        meta:{
            isBlack:false,
            isAuth: true
        }
    }

]

const router = createRouter({
    warnings: false,
    history: createWebHistory(),
    routes: routes
})
router.beforeEach((to, from, next) => {
    const {userIsLogin,openLoginDialog,setBeforeLoginRoutePath} = useLoginStore()
    if (to.meta.isAuth) {
        console.log("before")
        console.log(userIsLogin())
        if (!userIsLogin()) {
            openLoginDialog()
            setBeforeLoginRoutePath(to.path)
            return;
        }
        next()
    }
    next()

})
export default router